<?php
/**
 * Created by PhpStorm.
 * User: gaojie
 * Date: 2019/12/30
 * Time: 3:55 PM
 */

namespace app\Console\Commands;

use App\Model\Device;
use App\Model\DeviceThree;
use App\Model\DeviceTwo;
use App\Model\InverterData;
use App\Model\InverterDataThree;
use App\Model\InverterDataTwo;
use App\Model\Site;
use App\Model\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RecordYear extends Command
{


    protected $signature = 'RecordYear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计年';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function handle(){

        $sites = Site::all();
        $date = date('Y-m-d H:i:s');
        foreach ($sites as $site) {
            $user_id = $site->user_id;
            $site_id = $site->id;
            switch ($user_id) {
                case $user_id < 17:
                    $data = InverterData::getLastInverterData($user_id);
                    $year_gen_cap = $data['total_gen_cap'] - InverterData::getYearGenCapByUserId($user_id) - $data['total_offset'];
                    $offset = $data['total_offset'];
                    break;
                case $user_id >= 17 && $user_id < 43;
                    $data = InverterDataTwo::getLastInverterData($user_id);
                    $year_gen_cap = $data['total_gen_cap'] - InverterDataTwo::getYearGenCapByUserId($user_id) - $data['total_offset'];
                    $offset = $data['total_offset'];
                    break;
                default:
                    $data = InverterDataThree::getLastInverterData($user_id);
                    $offset = $data['total_offset'];
                    $year_gen_cap = $data['total_gen_cap'] - InverterDataThree::getYearGenCapByUserId($user_id) - $data['total_offset'];
            }
            DB::insert('insert into test_year_gen (year_gen,site_id,event_time,offset) values (?,?,?,?)', [$year_gen_cap,$site_id,$date,$offset]);
        }
    }


}